﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Server.CacheData;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    public class ReqChangeControlStateEventHandler : BaseHandler<ReqChangeControlStateEventData, RespChangeControlStateEventData>
    {
        public ReqChangeControlStateEventHandler()
            : base(RESPONSE_SCENARIO.SETTING, RESPONSE_COMMAND.SETTING.CHANGECONTROLSTATE)
        {
        }

        protected override RespChangeControlStateEventData OnHandle(Data.MainServiceData.Request request, ReqChangeControlStateEventData requestData)
        {
            CacheData.Client client = ServerData.Instance.GetClient(request.Header.UserIdentity);
            if (client == null)
            {
                Logger.ErrorToTag("MainService", string.Format("用户:{0}更改控制状态失败，用户不存在！", request.Header.UserIdentity));
                return null;
            }
            if (requestData.ControlState == Service.Contract.ControlState.UnderControl)
            {
                client.ControlState = CacheData.ControlState.UnderControl;
            }
            else
            {
                client.ControlState = CacheData.ControlState.OutOfControl;
            }
            RespChangeControlStateEventData respData = new RespChangeControlStateEventData(
                requestData.ControlState,
                requestData.UserIdentity,
                requestData.Solution,
                requestData.DomainId);
            return respData;
        }
    }
}
